<?php
/**
 * created by zzy
 * date: 2017/10/20 17:01
 */

namespace App\Eloquent\Ygt;

class Sms extends DbEloquent{

    protected   $table    = 'ygt_sms';

    //短信是否已经发送
    public static function isSend( $mobile, $limitTime=0){

        $where              = ['mobile'=>$mobile];
        $info               = self::getInfo( $where );
        $result             = false;
        if ($info) {
            if( $limitTime > 0 ){
                $infoArr       = $info->toArray();
                $addTime       = $infoArr['created_at'];
                $endTime       = $addTime + $limitTime;
                $nowTime       = $_SERVER['REQUEST_TIME'];
                if ($endTime > $nowTime) {
                    $result     = true;
                }
            } else {
                $result         = true;
            }
        }
        return $result;
    }
    public static function checkCode( $mobile, $code, $type, $limitTime=300 ){

        $where                  = ['mobile'=>$mobile,'type'=>$type];
        $info                   = self::getInfo( $where );
        $result                 = false;
        if( $info ){
            $infoArr           = $info->toArray();
            $result             = true;
            $smsCode           = $infoArr['code'];
            if( $smsCode != $code ){
                $result         = false;
            }
            if( $limitTime > 0 ){
                $addTime       = $infoArr['created_at'];
                $endTime       = $addTime + $limitTime;
                $nowTime       = $_SERVER['REQUEST_TIME'];
                if ($endTime < $nowTime) {
                    $result     = false;
                }
            }
        }
        return $result;
    }
}
